<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/14
 * Time: 13:23
 */

namespace data\model;
use traits\model\SoftDelete;

class Article extends BaseModel
{
    use SoftDelete;
    protected $autoWriteTimestamp = true;
    protected $createTime ='time';
//    protected $hidden = ['content','cataid','commentid','click'];
    public function catas(){
        return $this->hasOne('Cata','cataid','cataid')->field('cataid,cataname');
    }
    /**
     * 查询上一篇文章
     */
    public function pre($id,$cataid){
        $res = self::with('catas')
            ->where('artid','<',$id)
            ->where('cataid',$cataid)
            ->order('artid','desc')
            ->limit(1)
            ->find();
        return $res;
    }
    /**
     * 查询上一篇文章
     */
    public function next($id,$cataid){
        $res = self::with('catas')
            ->where('artid','>',$id)
            ->where('cataid',$cataid)
            ->order('artid','asc')
            ->limit(1)
            ->find();
        return $res;
    }

    /**
     * 相关文章
     */
    public function relevant($id,$tag2){
     $res =  $this ->where('artid','<>',$id)
            ->where('tag','like',$tag2,'or')
            ->select();
        return $res;
    }
}